using PK.Broadcaster;
using PKGames.Voxel;
using System.Collections;
using UnityEngine;

namespace PKGames
{
	[CreateAssetMenu(fileName = "RotationTransition", menuName = "Window Transitions/RotationTransition", order = 0)]
	public class RotationTransition : WindowTransition
	{
		[SerializeField]
		private AnimationCurve _fadeCurve;

		[SerializeField]
		private float _animationTime = 1f;

		[SerializeField]
		private Animator _rotationHelper;

		public override IEnumerator BeginOpenWindowTransition(RectTransform targetTransform)
		{
			if (_nextWindow != null)
			{
				_nextWindow.transform.position = targetTransform.position;
				_nextWindow.WindowCanvasGroup().alpha = 0f;
				_nextWindow.WindowHolder.SetActive(value: true);
			}
			GameObject rotationHelperObject = CreateRotationHelperObject();
			Animator rotationHelper = rotationHelperObject.GetComponent<Animator>();
			yield return AddRotationHelperToScene(rotationHelper);
			yield return RotateScreen();
			yield return FadeOutRotationHelper(rotationHelper);
			rotationHelperObject.transform.SetParent(null);
			UnityEngine.Object.Destroy(rotationHelperObject);
			yield return base.BeginOpenWindowTransition(targetTransform);
		}

		private GameObject CreateRotationHelperObject()
		{
			GameObject gameObject = Object.Instantiate(_rotationHelper.gameObject);
			if (_nextWindow != null)
			{
				gameObject.transform.SetParent(_nextWindow.WindowHolder.transform.parent, worldPositionStays: false);
			}
			else
			{
				gameObject.transform.SetParent(_lastWindow.WindowHolder.transform.parent, worldPositionStays: false);
			}
			gameObject.GetComponent<RectTransform>().anchorMin = Vector2.zero;
			gameObject.GetComponent<RectTransform>().anchorMax = Vector2.one;
			gameObject.GetComponent<RectTransform>().offsetMin = Vector2.zero;
			gameObject.GetComponent<RectTransform>().offsetMax = Vector2.zero;
			gameObject.GetComponent<RectTransform>().localScale = Vector3.one;
			gameObject.GetComponent<RectTransform>().localPosition = Vector3.zero;
			return gameObject;
		}

		private IEnumerator AddRotationHelperToScene(Animator rotationHelper)
		{
			rotationHelper.SetBool("Enabled", value: true);
			yield return new WaitForSeconds(0.2f);
		}

		private IEnumerator RotateScreen()
		{
			if (ApplicationManager.GetScreenOrientation() == ScreenOrientation.Portrait)
			{
				MessageBroadcaster.Instance.BroadcastMessage(new OnOrientationChangeMessage(OnOrientationChangeMessage.ScreenOrientation.Landscape));
				Screen.orientation = ScreenOrientation.LandscapeLeft;
			}
			else
			{
				MessageBroadcaster.Instance.BroadcastMessage(new OnOrientationChangeMessage(OnOrientationChangeMessage.ScreenOrientation.Portrait));
				Screen.orientation = ScreenOrientation.Portrait;
			}
			yield return new WaitForSeconds(0.1f);
		}

		private IEnumerator FadeOutRotationHelper(Animator rotationHelper)
		{
			rotationHelper.SetBool("Enabled", value: false);
			if (_nextWindow != null)
			{
				_nextWindow.WindowCanvasGroup().alpha = 1f;
			}
			else
			{
				_lastWindow.WindowCanvasGroup().alpha = 0f;
			}
			yield return new WaitForSeconds(0.2f);
		}

		public override IEnumerator BeginCloseWindowTransition(RectTransform targetTransform)
		{
			yield return BeginOpenWindowTransition(targetTransform);
		}
	}
}
